package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/16 16:36
 * description:
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<DtsAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        IPage<DtsAdmin> ipage = new Page<>(page, limit);

        //设置查询条件
        QueryWrapper<DtsAdmin> wapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wapper.like("username", username);
        }
        wapper.eq("deleted", false);

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }
        }

        //分页查询
        IPage<DtsAdmin> pageResult = adminMapper.selectPage(ipage, wapper);

        return pageResult;
    }

    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> wapper = new QueryWrapper<>();
        wapper.eq("username", username);
        wapper.eq("deleted", false);
        List<DtsAdmin> list = adminMapper.selectList(wapper);
        return list;
    }

    @Override
    public void add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insert(admin);
    }

    @Override
    public int updateById(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        return adminMapper.updateById(admin);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }
    @Override
    public DtsAdmin findAdminById(Integer id) {
        return adminMapper.selectById(id);
    }
}
